package com.xywy.tangandroid.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.tangandroid.R;
import com.xywy.tangandroid.fragment.BloodInstructionFragement;
import com.xywy.tangandroid.fragment.MyWearFragement;

public class KangBaobeiFrameAct extends FragmentActivity implements
		OnClickListener {
	private RelativeLayout left_img;
	private TextView middle_title;
	private FragmentManager fm;
	
	private MyWearFragement wearFragement;
	private BloodInstructionFragement instructionFragement;
	private TextView my_wear;
	private TextView instruction;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_kangbaobei);
		initview();
		initclick();
		fm = getSupportFragmentManager();
		switchFragment(R.id.my_wear);

	}

	private void initview() {
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources().getString(R.string.kangbaby));
		my_wear = (TextView)findViewById(R.id.my_wear);
		instruction = (TextView)findViewById(R.id.instruction);
	}

	private void initclick() {
		left_img.setOnClickListener(this);
		my_wear.setOnClickListener(this);
		instruction.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
          switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.my_wear:
			switchFragment(R.id.my_wear);
			break;
		case R.id.instruction:
			switchFragment(R.id.instruction);
			break;
		
		default:
			break;
		}
	}
	/**
	 * 锟斤拷锟斤拷时锟斤拷谋锟絝ragement锟斤拷状态
	 * 
	 * @param id
	 */
	private void switchFragment(int id) {
		FragmentTransaction fragmentTransaction = fm.beginTransaction();
		// 锟斤拷锟斤拷锟截碉拷锟斤拷锟叫碉拷Fragment锟斤拷锟皆凤拷止锟叫讹拷锟紽ragment锟斤拷示锟节斤拷锟斤拷锟较碉拷锟斤拷锟�
		hideFragments(fragmentTransaction);
		switch (id) {
		case R.id.my_wear:

			my_wear
					.setBackgroundColor(0xff999999);
			my_wear.setTextColor(0xffffffff);
			instruction.setBackgroundColor(0xffffffff);
			instruction.setTextColor(0xff999999);
			if (wearFragement == null) {
				wearFragement = new MyWearFragement();
				fragmentTransaction.add(R.id.content_frame, wearFragement);
			} else {
				fragmentTransaction.show(wearFragement);
			}

			break;
		case R.id.instruction:
			my_wear
			.setBackgroundColor(0xffffffff);
			my_wear.setTextColor(0xff999999);
	         instruction.setBackgroundColor(0xff999999);
	         instruction.setTextColor(0xffffffff);

			if (instructionFragement == null) {
				instructionFragement = new BloodInstructionFragement();
				fragmentTransaction.add(R.id.content_frame, instructionFragement);
			} else {
				fragmentTransaction.show(instructionFragement);
			}
			break;

		}
		fragmentTransaction.commit();
	}

	/**
	 * 锟斤拷锟斤拷锟叫碉拷Fragment锟斤拷锟斤拷为锟斤拷锟斤拷状态锟斤拷
	 * 
	 * @param transaction
	 *            锟斤拷锟节讹拷Fragment执锟叫诧拷锟斤拷锟斤拷锟斤拷锟�
	 */
	private void hideFragments(FragmentTransaction ft) {
		if (wearFragement != null) {
			ft.hide(wearFragement);
		}
		if (instructionFragement != null) {
			ft.hide(instructionFragement);
		}
		
	}
}
